package org.example.smartbuy.controller;

import org.example.smartbuy.Result;
import org.example.smartbuy.entity.Payments;
import org.example.smartbuy.service.IPaymentsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/payments")
@Tag(name = "Payments Controller", description = "Operations related to payments")
public class PaymentsController {

    @Autowired
    private IPaymentsService paymentsService;

    /**
     * 获取所有支付记录
     * @return List<Payments>
     */
    @GetMapping("/list")
    @Operation(summary = "Get all payment records", description = "Fetch a list of all payment records",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result listAllPayments() {
        return Result.success(paymentsService.listAll());
    }

    /**
     * 根据ID查询单个支付记录
     * @param id 支付记录ID
     * @return Payments
     */
    @GetMapping("/{id}")
    @Operation(summary = "Get a payment record by ID", description = "Fetch payment by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result getPaymentById(@PathVariable int id) {
        return Result.success(paymentsService.getById(id));
    }

    /**
     * 添加支付记录
     * @param payments 支付记录对象
     * @return Payments
     */
    @PostMapping("/add")
    @Operation(summary = "Add a new payment record", description = "Create a new payment record",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result addPayment(@RequestBody Payments payments) {
        return Result.success(paymentsService.add(payments));
    }

    /**
     * 删除支付记录
     * @param id 支付记录ID
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "Delete a payment record", description = "Delete a payment record by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result deletePayment(@PathVariable int id) {
        paymentsService.delete(id);
        return Result.success();
    }
}
